import axios from 'axios'
import {Modal,message} from 'antd';
// import qs from '../libs/index';
const { confirm } = Modal;
var http = {}
http.imgaddress = 'http://192.168.1.200:8000/file/image/';
http.imgimgadress = 'http://192.168.1.200:8000/file';
const mianurl = '/app';
const userinfo = JSON.parse(localStorage.getItem('userinfo'))||''
http.post =function post(url,data){
    axios.defaults.headers.common['token'] = userinfo.text;
    return new Promise(function (resolve,reject) {
        axios.post(mianurl + url,data,
            {headers:{'X-Requested-With':'XMLHttpRequest','Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        }).then((res)=>{
            if(res.data === 401){
                window.location.href='#/login';
            }else if(res.data.success){
                resolve(res.data);
            }else{
                message.error('请求失败,检查操作');
                reject('请求失败')
            }
        }).catch(function (error) {
            console.log(error);
        });
    })
    
}
http.delect =(url,data,requstType)=>{
    return new Promise(function(resolve,reject){
        confirm({
            title: '确认删除？',
            content: '请谨慎操作',
            okText: '删除',
            okType: 'danger',
            cancelText: '取消',
            onOk() {
                axios.post(mianurl + url,data,{headers:{'X-Requested-With':'XMLHttpRequest','Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
                }).then(function (res) {
                    if(res.data === 401){
                        window.location.href='#/login';
                    }else if(res.data.success){
                        resolve(res.data);
                        message.success('删除成功')
                    }else{
                        message.error('请求失败,检查操作');
                        reject('请求失败')
                    }
                })
                .catch(function (error) {
                    
                });
            }
        }); 
    })
    
}

/* 表单内容 */
http.formItemLayout = {labelCol: {span:6},wrapperCol: { span: 17}};
http.formItemTextarea = {labelCol: {span:3},wrapperCol: { span: 21}};
http.formItemCustom = {labelCol: { span:24},wrapperCol: { span: 24}};
http.customPanelStyle = {background: '#f7f7f7', borderRadius: 4,marginBottom: 24,border: 0,overflow: 'hidden',};
http.dateFormat = 'YYYY/MM/DD';
/* 正则验证 */
http.label = /^[a-zA-Z\u4e00-\u9fa5]{2,6}$/; /* label正则验证 */
http.head=/^[a-zA-Z\u4e00-\u9fa5]{2,15}$/;   /* 标题正则验证 */
http.subhead=/^[a-zA-Z\u4e00-\u9fa5]{4,30}$/;/* 副标题验证 */
http.code=/^[a-zA-Z\u4e00-\u9fa5]{4,30}$/;   /* code验证 */
http.textarea=/^.{5,100}$/;                  /* 多行文本验证 */
export default http
